//
//  Pay.swift
//  mosdks
//
//  Created by zhu xietong on 2020/4/16.
//

import Foundation
import Eelay


public enum PayStatus {
    case waiting
    case success
    case failed
}

public enum PayTask {
    case GoodsOrder
    case Pads
}

//
//public enum SeverPayMethod:String {
//
//    case wxPay = "1"
//    case aliPay = "2"
//    case cardPay = "3"
//
//
//    public static var defaultMethod:SeverPayMethod = .wxPay
//
//    public var options:[String:Any]{
//        get{
//            return ["name":self.name,"icon":self.icon,"id":self.rawValue]
//        }
//    }
//
//    public static func method(with ID:String)->PayMethod{
//        return PayMethod(rawValue: ID) ?? .defaultMethod
//    }
//
//
//
//    public var name:String{
//        get{
//            switch self {
//            case .aliPay:
//                return "支付宝"
//            case .wxPay:
//                return "微信支付"
//            case .cardPay:
//                return "银行卡支付"
//
//            }
//        }
//    }
//
//    public var icon:String{
//        switch self {
//        case .wxPay:
//            return "jd_zf_wx"
//        case .aliPay:
//            return "jd_zf_zfb"
//        default:
//            return "jd_zf_yh"
//        }
//    }
//
//    public static var payOptions:[[String:Any]]{
//        get{
//            let o = [
//                PayMethod.wx.options,
//                PayMethod.ali.options,
////                    PayMethod.cardPay.options,
//            ]
//            return o
//        }
//    }
//}


extension SDKConfig{
    public struct ZifuBao{
        public static var scheme = "momall"
    }
}

public protocol MoPayRealization {
    var name:String{get set}
    var ID:String{get set}
    func pay(pay_id:String,task_type:String,_ sign:NSMutableDictionary,finish_block:@escaping (DoPayReuslt,String,String)-> Void)

}


public enum PayMethod:String {
    
    case wx = "wx"
    case ali = "ali"
    case card = "card"
    case wallet = "wallet"

    
    
    
    public var signNode:String{
        switch self {
        case .wx:
            return "wechat"
        default:
            return "alipay"
        }
    }
    public static var defaultMethod:PayMethod = .wx
    
    public var options:[String:Any]{
        get{
            return ["name":self.name,"icon":self.icon,"id":self.rawValue]
        }
    }
    
    public static func method(with ID:String)->PayMethod{
        return PayMethod(rawValue: ID) ?? .defaultMethod
    }
    
    public var name:String{
        get{
            switch self {
            case .ali:
                return "支付宝"
            case .wx:
                return "微信支付"
            case .wallet:
                return "钱包支付"
            case .card:
                return "银行卡支付"
            }
        }
    }
    
    public var icon:String{
        switch self {
        case .wx:
            return "jd_zf_wx"
        case .ali:
            return "jd_zf_zfb"
        default:
            return "jd_zf_yh"
        }
    }
    
    public static var payOptions:[[String:Any]]{
        get{
            let o = [
                PayMethod.wx.options,
                PayMethod.ali.options,
//                PayMethod.cardPay.options,
                ]
            return o
        }
    }
}



public enum DoPayReuslt
{
    case ok(PayMethod)
    case canle(PayMethod)
    case failed(PayMethod,String)
}


public class DoPay {
    static var _share:DoPay? = nil
    
    public static var share:DoPay{
        get{
            guard let share = _share else {
                let share = DoPay()
                _share = share
                return share
            }
            return share
        }
    }
    
    public func add(realization:MoPayRealization) {
        var have = false
        for r in realizations {
            if r.ID == realization.ID{
                have = true
            }
        }
        if !have{
            realizations.append(realization)
        }
    }
    
    public var realizations:[MoPayRealization] = []
    func realization(ID:String)->MoPayRealization?{
//        print("----IIIIS----s",realizations)
        for realization in realizations {
//            print("_____S",ID,realization.ID)
            if realization.ID == ID{
                return realization
            }
        }
        return nil
    }
    
    public static func pay(_ method:String,pay_id:String,task_type:String,_ sign:NSMutableDictionary,finish_block:@escaping (DoPayReuslt,String,String)-> Void) {
        guard let _method = PayMethod(rawValue: method) else{return}
        
//        print("oooi----\(_method)====ss")
        if let real = DoPay.share.realization(ID: method){
//            print("sdasd---",real)
            real.pay(pay_id: pay_id, task_type: task_type, sign, finish_block: finish_block)
        }
        
        
    }

}
